package com.fattyCorps.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fattyCorps.cache.PlayerCache;
import com.fattyCorps.config.gameConfig.GlobalMailConfig;
import com.fattyCorps.exception.gameException.item.ItemIdErrorException;
import com.fattyCorps.exception.gameException.mail.MailAlreadyGetAttachmentErrorException;
import com.fattyCorps.exception.gameException.mail.MailIdErrorException;
import com.fattyCorps.exception.gameException.mail.MailNoAttachmentErrorException;
import com.fattyCorps.mapper.MailMapper;
import com.fattyCorps.obj.cache.PlayerInfo;

import com.fattyCorps.obj.db.Mail;
import com.fattyCorps.obj.mould.GlobalMailMould;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MailService {

    @Autowired
    PlayerCache playerCache;

    @Autowired
    ItemService itemService;

    @Autowired
    GlobalMailConfig globalMailConfig;

    @Autowired
    MailMapper mailMapper;

    // 对比用户当前已获取的全服邮件id，若小与配置的全服邮件id则给用户发放未接收的全服邮件
    public void procGlobalMail(PlayerInfo playerInfo) {
        Integer playerId = playerInfo.getBaseProp().getId();
        Integer globalMailId = playerInfo.getBaseProp().getGlobalMailId();

        // 如果用户身上的全服邮件id小于当前最新的全服邮件id，需要将所有未发放给用户的全服邮件发给用户
        if (globalMailId < globalMailConfig.getMaxGlobalMailId()) {
            List<GlobalMailMould> disGlobalMailList = globalMailConfig.getDisGlobalMailList(globalMailId);

            // 发放邮件
            for (GlobalMailMould globalMailMould : disGlobalMailList) {
                addMail(playerId, globalMailMould.getTitle(), globalMailMould.getContent(), globalMailMould.getAttachment());
            }

            // 修改用户身上的全服邮件id属性
            playerInfo.getBaseProp().setGlobalMailId(globalMailConfig.getMaxGlobalMailId());
        }
    }

    // 读取邮件
    public void readMail(Integer playerId, Integer mailId) {
        Mail mail = getMail(playerId, mailId);
        mail.setRead(true);
    }

    // 领取附件
    public void getAttachment(Integer playerId, Integer mailId) {
        Mail mail = getMail(playerId, mailId);

        // 判断是否有附件
        if (mail.getAttachment().isEmpty()) {
            throw new MailNoAttachmentErrorException();
        }

        // 是否附件已经领取
        if (mail.isGetAttachment()) {
            throw new MailAlreadyGetAttachmentErrorException();
        }

        // 解析邮件附件参数,增加附件道具
        // 附件格式：道具1typeId,道具1数量,道具2typeId,道具2数量。。。
        String[] split = mail.getAttachment().split(",");
        Integer itemTypeId = 0;
        Integer num = 0;

        for (int i = 0; i < split.length; i++) {
            if (i % 2 == 0) {
                // 道具typeId
                itemTypeId = Integer.valueOf(split[i]);
            } else {
                num = Integer.valueOf(split[i]);
                itemService.addItem(playerId, itemTypeId, num);
            }
        }

        // 设置附件为已领取
        mail.setGetAttachment(true);
    }

//    下列getMail代码已优化,详见playerCache.getElemById
//    pr Mail getMail(Integer playerId, Integer id) {
//        List<Mail> mails = playerCache.getPlayerInfo(playerId).getMails();
//        for (int i = 0; i < mails.size(); i++) {
//            Mail mail = mails.get(i);
//            if (mail.getId().equals(id)) {
//                return mail;
//            }
//        }
//        throw new ItemIdErrorException();
//    }

    // 优化后getMail代码
    public Mail getMail(Integer playerId, Integer mailId) {
        List<Mail> mails = playerCache.getPlayerInfo(playerId).getMails();
        return playerCache.getElemById(mails, mailId, new MailIdErrorException());
    }

    public List<Mail> getMailList(Integer playerId) {
        return playerCache.getPlayerInfo(playerId).getMails();
    }

    public void addMail(Integer playerId, String title, String content, String attachment) {
        Mail mail = new Mail();
        mail.setPlayerId(playerId);
        mail.setTitle(title);
        mail.setContent(content);
        mail.setAttachment(attachment);
        mailMapper.insert(mail);
    }
}
